package com.gitee.zhuyb;

import com.gitee.zhuyb.domain.PageUtil;
import com.gitee.zhuyb.domain.PageVo;
import com.gitee.zhuyb.domain.SysUser;
import com.gitee.zhuyb.mapper.SysUserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @version 1.0.0
 * @Description: #分页测试类
 * @Date: 2021/10/30 14:57
 * @Copyright (C) ZhuYouBin
 */
public class TestPage {
    public static void main( String[] args ) throws IOException {
        // 读取mybatis-config.xml文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 初始化mybatis，创建SqlSessionFactory类的实例
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 创建Session实例
        SqlSession session = sqlSessionFactory.openSession();
        // 获得mapper接口的代理对象
        SysUserMapper sysUserMapper = session.getMapper(SysUserMapper.class);

        // 开启分页
        PageVo pageVo = new PageVo(0, 2);
        PageUtil.start(pageVo);

        // 查询数据库
        SysUser sysUser = new SysUser();
        List<SysUser> sysUserLists = sysUserMapper.queryUser(sysUser);

        // 分页结束
        PageVo end = PageUtil.end();
        System.out.println("输出分页结果对象: " + end);

        // 提交事务
        session.commit();
        // 关闭Session
        session.close();
    }
}
